package mo.leetcode_top;

import java.util.Arrays;

public class L73 {

    public void setZeroes(int[][] matrix) {

        boolean firstColumnZero = false;
        boolean firstRowZero = false;

        
        for (int i = 0; i < matrix.length; i++) {
            if (matrix[i][0] == 0) {
                firstRowZero = true;
                break;
            }
        }

        for (int i = 0; i < matrix[0].length; i++) {
            if (matrix[0][i] == 0) {
                firstColumnZero = true;
                break;
            }
        }

        for (int i = 1; i < matrix.length; i++) {
            for (int j = 1; j < matrix[0].length; j++) {
                if (matrix[i][j] == 0) {
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }
            }
        }

        for (int i = 1; i < matrix.length; i++) {
            for (int j = 1; j < matrix[0].length; j++) {
                if (matrix[i][0] == 0 || matrix[0][j] == 0) {
                    matrix[i][j] = 0;
                }
            }
        }

        for (int i = 0; i < matrix.length; i++) {
            if (firstRowZero) {
                matrix[i][0] = 0;
            }
        }

        for (int i = 0; i < matrix[0].length; i++) {
            if (firstColumnZero) {
                matrix[0][i] = 0;
            }
        }
    }

    public static void main(String[] args) {
        L73 test = new L73();
        int[][] data = {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}};
        test.setZeroes(data);
        System.out.println(Arrays.deepToString(data));
    }
}
